package ap.project.controller.service;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Handler;
import android.view.SurfaceHolder;

public class GameThread extends Thread {

	private SurfaceHolder mSurfaceHolder;

	// for consistent rendering
	public int state = 1;
	public final static int RUNNING = 1;
	public final static int PAUSED = 2;
	public final static int STOPPED = 3;
	
	private GameSurface gEngine;

	/**
	 * 
	 * @param surfaceHolder
	 * @param context
	 * @param handler
	 * @param gEngineS
	 */
	public GameThread(SurfaceHolder surfaceHolder, Context context,
			Handler handler, GameSurface gEngineS) {
		// data about the screen
		mSurfaceHolder = surfaceHolder;
		gEngine = gEngineS;		
	}

	@Override
	public void run() {
		while (state == RUNNING) {
			// time before update			

			Canvas c = null;
			try {
				c = mSurfaceHolder.lockCanvas(null);
				synchronized (mSurfaceHolder) {
					c.drawColor(Color.rgb(10, 78, 136));
					gEngine.doDraw(c);
				}
			} finally {
				if (c != null) {
					mSurfaceHolder.unlockCanvasAndPost(c);
				}
			}

			while (state == PAUSED) {
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}
